using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using System;
using MyApi.Api.Entity;
using MyApi.Api.Db;
using Microsoft.EntityFrameworkCore;

namespace MyApi.Api.Repository
{
    public class EfRepository<T> : IRepository<T>where T :BaseEntity
    {

        private MyApiDb _db;

        public EfRepository(MyApiDb db){
            _db=db;
        }

        public DbSet<T>_entity;
        protected DbSet<T> Entity{
            get{
                if(_entity==null){
                    _entity=_db.Set<T>();
                }
                return _entity;
            }
        }
        public IQueryable<T> Table{
            get{
                //AsQueryable支持数据库查询语法
                return Entity.AsQueryable<T>();
            }
        }
 public void Delete(int id)
        {
            var t = Entity.Where(x => x.Id == id).FirstOrDefault();
            Entity.Remove(t);
            _db.SaveChanges();
        }

        public T GetById(int id)
        {
            var t = Entity.Where(x => x.Id == id).FirstOrDefault();
            return t;
        }

        public void Insert(T entity)
        {
            if (entity == null)
            {
                throw new System.NullReferenceException();
            }

            entity.IsActived = true;
            entity.IsDeleted = false;
            entity.CreatedTime = DateTime.Now;
            entity.UpdatedTime = DateTime.Now;
       

            Entity.Add(entity);
            _db.SaveChanges();
        }

        public async Task InsertAsync(T entity)
        {
            if (entity == null)
            {
                throw new System.NullReferenceException();
            }

            entity.IsActived = true;
            entity.IsDeleted = false;
            entity.CreatedTime = DateTime.Now;
            entity.UpdatedTime = DateTime.Now;


            await Entity.AddAsync(entity);
            await _db.SaveChangesAsync();
        }

        public void InsertBulk(IEnumerable<T> entities)
        {
            foreach (var entity in entities)
            {
                entity.IsActived = true;
                entity.IsDeleted = false;
                entity.CreatedTime = DateTime.Now;
                entity.UpdatedTime = DateTime.Now;

            }
            Entity.AddRange(entities);
            _db.SaveChanges();
        }

        public async Task InsertBulkAsync(IEnumerable<T> entities)
        {
            foreach (var entity in entities)
            {
                entity.IsActived = true;
                entity.IsDeleted = false;
                entity.CreatedTime = DateTime.Now;
                entity.UpdatedTime = DateTime.Now;
        
            }
            await Entity.AddRangeAsync(entities);
            await _db.SaveChangesAsync();
        }

        public void Update(T entity)
        {
            if (entity == null)
            {
                throw new System.NullReferenceException();
            }

            entity.UpdatedTime = DateTime.Now;
            _db.SaveChanges();
        }

        public void UpdateBulk(IEnumerable<T> entities)
        {
            foreach (var entity in entities)
            {
                entity.UpdatedTime = DateTime.Now;
            }


            _db.SaveChanges();
        }
       
    }
}